import { EventManager } from "../../engine/EventManager";
import { JhaoProto } from "../../lib/net/protobuf/jhaoproto";
import { EventConfig } from "../config/EventConfig";
import { RoomPlayer } from "./RoomPlayer";
import { UserModel } from "./UserModel";

export class RoomModel {

    public static instance: RoomModel = new RoomModel()

    public room: JhaoProto.IRoomModel;

    private static initOneRoom(obj: object) {
        let model: RoomModel = new RoomModel()
        // model.room_id = obj["room_id"]
        // model.create_time = new Date(obj["create_time"]).getTime()
        // model.state = obj["state"]
        // model.clubid = obj["clubid"]
        // model.num_of_turns = obj["num_of_turns"]
        // if (!model.num_of_turns) {
        //     model.num_of_turns = 0
        // }
        // model.gameType = obj["gameType"]
        // model.server_id = obj["server_id"]
        // model.playerList = []
        // let playerObjList: Array<object> = []
        // model.conf = obj["conf"]
        // model.roomType = model.conf["roomType"]

        // model.game_info = obj["game_info"]
        // playerObjList = obj["playerList"]
        // model.initPlayerList(playerObjList);
        // Logger.log("initOneRoom===", obj, model)
        return model
    }

    public static initMyOneRoom(obj: JhaoProto.IRoomModel) {
        RoomModel.instance.room = obj;
        UserModel.instance.room_id = obj.roomId
        return RoomModel.instance
    }

    public initPlayerList(playerList: Array<object>) {
        // for (let i = 0; i < playerList.length; i++) {
        //     if (playerList[i]) {
        //         let player: RoomPlayer = RoomPlayer.createPlayerByObj(playerList[i])
        //         this.playerList[i] = player;
        //     } else {
        //         this.playerList[i] = null
        //     }
        // }
        EventManager.instance.dispatch(EventConfig.Event_Update_Room_Player)
    }

    public clear() {
        UserModel.instance.room_id = 0
    }

    public static initClubRoomList(objList: Array<object>) {
        // this.clubRoomList = []
        // for (let i = 0; i < objList.length; i++) {
        //     let model: RoomModel = this.initOneRoom(objList[i])
        //     this.clubRoomList[i] = model;
        // }
    }

    public getPlayerNum() {
        let playerNum: number = 0
        // for (let i = 0; i < this.playerList.length; i++) {
        //     if (this.playerList[i]) {
        //         playerNum++
        //     }
        // }
        return playerNum;
    }

}